import { Packet } from 'packet-model.js';
var packet = new Packet();
import { Center } from '../center/center-model.js';
var center = new Center();
import { Config } from '../../utils/config.js';
Page({
  data: {
    args:{
      money : 0,
      send_num:0,
      type:1
    },
    account : 0.00,
    btnArray:['生成红包','生成中'],
    btnText: "生成红包",
    onLoading : false,
    showShare:false,
  },
  onShow: function () {
    var config = wx.getStorageSync('config');
    this.data.site_name = config[2].value;
    this.setData({
      domain:Config.domain
    })
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var args = {};
    center.index(args, (data) => {
      wx.hideLoading();
      that.setData({
        account : data.item.user_money
      })
    })
  },
  //生成红包
  send:function(){
    if (!this.data.args.money){
      wx.showToast({
        title: '请输入单个红包金额',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.args.money > 200) {
      wx.showToast({
        title: '红包金额不能大于200',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.args.money > this.data.account) {
      wx.showModal({
        title: '提示',
        content: '账户余额不足',
        confirmText:"去充值",
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../center/recharge?type=3'
            });
          } else if (res.cancel) {
          }
        }
      })
      return false;
    }
    if (!this.data.args.send_num) {
      wx.showToast({
        title: '请先输入个数',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.args.send_num > 100) {
      wx.showToast({
        title: '个数不能大于100',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(this.data.onLoading){
      return false;
    }
    var that = this;
    var args = that.data.args;
    that.setData({
      btnText:that.data.btnArray[1]
    })
    that.data.onLoading = true;
    packet.send(args, (data) => {
      that.setData({
        btnText: that.data.btnArray[0]
      })
      that.data.onLoading = false
      if(data.status == 1){
        that.setData({
          code: data.data.encryptCode,
          id: data.data.id,
          showShare: true 
        })
        
      }else{
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  //获取输入的单个金额
  money: function (e) {
    this.data.args.money = e.detail.value
  },
  //获取输入的个数
  num: function (e) {
    this.data.args.send_num = e.detail.value
  },
  //去到充值页面
  toRechange:function(){
    wx.navigateTo({
      url: '../center/recharge?type=3'
    });
  },
  //去到红包记录
  log: function () {
    wx.navigateTo({
      url: 'log'
    });
  },
  //关闭弹窗
  hideModal:function(){
    that.setData({
      showShare: false
    })
  },
  //分享红包
  onShareAppMessage: function (res) {
    var that = this;
    var name = that.data.site_name ? that.data.site_name : '首页';
    if (res.from != 'button') {
      return {
        title: name,
        path: '/pages/home/home',
      }
    } else {
      if (!that.data.code || !that.data.id){
        return false;
      }
      return {
        title: '我在' + name +'发了一个红包，快来领取吧',
        imageUrl: Config.domain+'/storage/setting/share.png',
        path: '/pages/packet/receive?code=' + that.data.code + '&id=' + that.data.id,
      }
    }
  }
})